var isXMLHttpRequest = false;
var xmlhttp = false;
var version_confirm;
var step_confirm;

function getHTTPObject() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {
    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer
      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) {
      // Failed to create required ActiveXObject
      try {
        // Try version supported by older versions
        // of Internet Explorer
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }
    return xmlreq;
}


function parsedata(data) {
	var split_data = data.split('$$');
	var arraycount = split_data.length;
	var data_arr = new Array();
	for (var i = 0; i < split_data.length; i++) {
		subdata = split_data[i];
		if (subdata == '') continue;
		
		data_arr[i] = new Array(2);
		split_subdata = subdata.split('||');
		data_arr[i][0] = split_subdata[0];
		data_arr[i][1] = split_subdata[1];
	}

	return(data_arr);
}


function putSelectData(selectobj, data_arr, key, startindex) {
	if (! startindex) startindex = 0;

	for (j = 0; j < data_arr.length; j++) {
		var key_data = data_arr[j][0];
		var val_data = data_arr[j][1];

		if (key_data == '') continue;

		var options = new Option(val_data, key_data, true, (key == key_data));

		selectobj.options[startindex++] = options;
	}
}

function setSelectData1(xmlhttp, selectobj, key, startindex) {
	var xmlDoc = xmlhttp.responseXML;
	var items = xmlDoc.getElementsByTagName("content");
	
	if (items.length < 1) {
		return;
	} else {
		
		var returnstr = items[0].firstChild.nodeValue;
		
		if ((selectobj) && (typeof(selectobj) == "object")) {
			var data_arr = parsedata(returnstr);
			putSelectData(selectobj, data_arr, key, startindex);
			return;
		} else {
			return returnstr;
		}
	}
}

function setSelectData(selectobj, key, url, startindex) {

	if (! startindex) startindex = 0;
	var retval = "";

	var xmlhttp = getHTTPObject();

	xmlhttp.open('POST', url, false);
	xmlhttp.onreadystatechange = function() {
	  if(xmlhttp.readyState==4 && xmlhttp.status == 200 && xmlhttp.statusText=='OK') {
			retval = setSelectData1(xmlhttp, selectobj, key, startindex);
		}
	}
	xmlhttp.send(null);
	
	if (isXMLHttpRequest) {
		retval = setSelectData1(xmlhttp, selectobj, key, startindex);
	}

	return (retval);
}


function clearSelectbox(selectobj) {

	listcount = selectobj.length;
	for (i = 0; i < listcount; i++) {
		selectobj.options[0] = null;
	}    
}



function setSPSelect(selectobj, key, topstr, s_key) {
	clearSelectbox(selectobj);

	var index = 0;
	if ((topstr) && (topstr != '')) {
		selectobj.options[index++] = new Option(topstr, "");
	}
	
	var url = "/jsp/ajax_cd_selectbox.jsp?s_key="+s_key;
	setSelectData(selectobj, key, url, index);
}
